#!/usr/bin/env perl

use strict;
use Data::Dumper;
use File::Find;
my @opts = @ARGV;
my @args;
my @miss;
my %o = (
	umask => oct(22),
);
while (@opts) {
	$_ = shift(@opts);
	if (/^-u(.*)$/) {
		$o{umask} = oct(length $1 ? $1 : shift(@opts));
	}
	elsif (/^--notest$/) {
		$o{do} = 1;
	}
	elsif (/^-/) {
		warn "Unknown option: $_\n";
	}
	else{
		-e $_
			? push @args, $_
			: push @miss, $_;
			
	}
}

#print Dumper \%o,\@args;
my $dir = oct(777) & ( ~$o{umask} );
my $fil = oct(666) & ( ~$o{umask} );

printf "umask = %04o\n",$o{umask};
printf "dir   = %04o\n",$dir;
printf "file  = %04o\n",$fil;
printf "files = @args\n";

warn "\nThe following objects are missing: @miss\n" if @miss;
die "No files to be normalized\n" unless @args;

if ($o{do}) {
	my $files = 0;
	my $dirs = 0;
	File::Find::find(sub {
		-d and ++$dirs and chmod $dir,$_;
		-f and ++$files and chmod $fil,$_;
	},@args);
	print "Done. $files files and $dirs directories were processed\n";
}else{
	print "To really execute run with option --notest\n";
}